package com.feonix.common.util.jdbc;

import com.feonix.common.annotation.ColumnType;
import com.feonix.common.basedao.EntityProperty;

public class DB2Dialect implements Dialect {
	public boolean supportsLimit() {
		return true;
	}

	private String getRowNumber(String sql) {
		StringBuffer sb = new StringBuffer(50).append("rownumber() over(");

		int i = sql.toLowerCase().indexOf("order by");

		if ((i > 0) && (!(hasDistinct(sql)))) {
			sb.append(sql.substring(i));
		}

		sb.append(") as rownumber_,");

		return sb.toString();
	}

	public String getLimitString(String sql, int offset, int limit) {
		int i = sql.toLowerCase().indexOf("select");

		StringBuffer sqlSb = new StringBuffer(sql.length() + 100).append(sql.substring(0, i))
				.append("select * from ( select ").append(getRowNumber(sql));

		if (hasDistinct(sql))
			sqlSb.append(" row_.* from ( ").append(sql.substring(i)).append(" ) as row_");
		else {
			sqlSb.append(sql.substring(i + 6));
		}

		sqlSb.append(" ) as temp_ where rownumber_ ");

		sqlSb.append("between ").append(offset + 1).append(" and ").append(limit);
		return sqlSb.toString();
	}

	private static boolean hasDistinct(String sql) {
		return (sql.toLowerCase().indexOf("select distinct") >= 0);
	}

	public String getDateTimeColumn(EntityProperty entityProperty) {
		return null;
	}

	public String getDateColumn(EntityProperty entityProperty) {
		return null;
	}

	public String getIntColumn(EntityProperty entityProperty) {
		return null;
	}

	public String getDoubleColumn(EntityProperty entityProperty) {
		return null;
	}

	public String getFloatColumn(EntityProperty entityProperty) {
		return null;
	}

	public String getVarcharColumn(EntityProperty entityProperty) {
		return null;
	}

	public String getCharColumn(EntityProperty entityProperty) {
		return null;
	}

	public String getTextColumn(EntityProperty entityProperty) {
		return null;
	}

	public String getBlobColumn(EntityProperty entityProperty) {
		return null;
	}

	public String getGeneratorColumn(EntityProperty entityProperty) {
		return null;
	}

	public String getCharset() {
		return null;
	}

	public String addColumn(String table, String column, ColumnType columnType, String length, String defaultVal) {
		return null;
	}

	public String updateColumn(String table, String oldColumn, EntityProperty entityProperty) {
		return null;
	}

}
